package priv.jesse.mall.web.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import priv.jesse.mall.entity.Classification;
import priv.jesse.mall.entity.OrderItem;
import priv.jesse.mall.entity.Product;
import priv.jesse.mall.entity.ProductComment;
import priv.jesse.mall.entity.pojo.ResultBean;
import priv.jesse.mall.service.ClassificationService;
import priv.jesse.mall.service.ProductCommentService;
import priv.jesse.mall.service.ProductService;
import priv.jesse.mall.service.ShopCartService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ClassificationService classificationService;
    @Autowired
    private ShopCartService shopCartService;
    @Autowired
    private ProductCommentService productCommentService;
    /**
     * 获取商品信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/get.do")
    public ResultBean<Product> getProduct(int id) {
        Product product = productService.findById(id);
        return new ResultBean<>(product);
    }


    /**
     * 获取商品信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/getpro.do")
    public String getPro(int id,Map<String, Object> map,HttpServletRequest request) {
        Product product = productService.findById(id);
        String urlprefix = request.getScheme() + "://"
                + request.getServerName() + ":"
                + request.getServerPort();
        String[] ss=product.getImage().split("&");
        String image=urlprefix.trim()+"/mall/admin/product/getImg.do?url="+ ss[0].replace("/","%2F").replace("\\","%2F");
        product.setImage(image);
        for(int i=0;i<ss.length;i++){
            String img=urlprefix.trim()+"/mall/admin/product/getImg.do?url="+ ss[i].replace("/","%2F").replace("\\","%2F");
            ss[i]=img;
        }
        product.setImagelist(ss);

        int count =  productCommentService.findCountById(product.getId());


        map.put("product", product);
        map.put("count", count);
        return "newmall/views";
    }


    /**
     * 打开商品详情页面
     *
     * @param id
     * @param map
     * @return
     */
    @RequestMapping("/get.html")
    public String toProductPage(int id, Map<String, Object> map) {
        Product product = productService.findById(id);
        map.put("product", product);
        return "mall/product/info";
    }

    /**
     * 查找热门商品
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/hot.do")
    public ResultBean<List<Product>> getHotProduct() {
        List<Product> products = productService.findHotProduct();
        return new ResultBean<>(products);
    }




    /**
     * 查找最新商品
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/new.do")
    public ResultBean<List<Product>> getNewProduct(int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findNewProduct(pageable);
        return new ResultBean<>(products);
    }

    /**
     * 打开分类查看商品页面
     *
     * @return
     */
    @RequestMapping("/category.html")
    public String toCatePage(int cid, Map<String, Object> map) {
        Classification classification = classificationService.findById(cid);
        map.put("category", classification);
        return "mall/product/category";
    }

    /**
     * 打开分类查看商品页面
     *
     * @return
     */
    @RequestMapping("/category_list.html")
    public String toCatelist(int csid,Map<String, Object> map) {
        map.put("csid", csid);
        return "newmall/category_list";
    }

    @RequestMapping("/getcategorylist.do")
    @ResponseBody
    public ResultBean<List<Product>> getcategorylist(int csid,String orderDir,HttpServletRequest request) {
        List<Product> list = new ArrayList<>();
         list= productService.findByCidAndType(csid,orderDir);
        String urlprefix = request.getScheme() + "://"
                + request.getServerName() + ":"
                + request.getServerPort();
        for(int i=0;i<list.size();i++){
        String[] ss=list.get(i).getImage().split("&");
        String image=urlprefix.trim()+"/mall/admin/product/getImg.do?url="+ ss[0].replace("/","%2F").replace("\\","%2F");
        list.get(i).setImage(image);
        }
        return new ResultBean<>(list);
    }

    @RequestMapping("/cengjihotlist.do")
    @ResponseBody
    public ResultBean<List<Product>> findCengjiHot(int type,HttpServletRequest request) {
        List<Product> list = new ArrayList<>();
        list = productService.findCengjiHot(type);
        String urlprefix = request.getScheme() + "://"
                + request.getServerName() + ":"
                + request.getServerPort();
        for(int i=0;i<list.size();i++){
            String[] ss=list.get(i).getImage().split("&");
            String image=urlprefix.trim()+"/mall/admin/product/getImg.do?url="+ ss[0].replace("/","%2F").replace("\\","%2F");
            list.get(i).setImage(image);
        }
        return new ResultBean<>(list);
    }



    @RequestMapping("/toCart.html")
    public String toCart(){
        return "mall/product/cart";
    }



    @RequestMapping("/toNewCart.html")
    public String toNewCart(){
        return "newmall/shopcart";
    }



    /**
     * 按一级分类查找商品
     *
     * @param cid
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/category.do")
    public ResultBean<List<Product>> getCategoryProduct(int cid, int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findByCid(cid, pageable);
        return new ResultBean<>(products);
    }

    /**
     * 按二级分类查找商品
     *
     * @param csId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/categorySec.do")
    public ResultBean<List<Product>> getCategorySecProduct(int csId, int pageNo, int pageSize) {
        Pageable pageable = new PageRequest(pageNo, pageSize);
        List<Product> products = productService.findByCsid(csId, pageable);
        return new ResultBean<>(products);
    }

    /**
     * 根据一级分类查询它所有的二级分类
     * @param cid
     * @return
     */
    @ResponseBody
    @RequestMapping("/getCategorySec.do")
    public ResultBean<List<Classification>> getCategorySec(int cid){
        List<Classification> list = classificationService.findByParentId(cid);
        return new ResultBean<>(list);
    }

    /**
     * 加购物车
     *
     * @param productId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addCart.do")
    public ResultBean<Boolean> addToCart(int productId, HttpServletRequest request) throws Exception {
        shopCartService.addCart(productId, request);
        return new ResultBean<>(true);
    }




    /**
     * 移除购物车
     *
     * @param productId
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/delCart.do")
    public ResultBean<Boolean> delToCart(int productId, HttpServletRequest request) throws Exception {
        shopCartService.remove(productId, request);
        return new ResultBean<>(true);
    }

    /**
     * 查看购物车商品
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/listCart.do")
    public ResultBean<List<OrderItem>> listCart(HttpServletRequest request) throws Exception {
        List<OrderItem> orderItems = shopCartService.listCart(request);
        return new ResultBean<>(orderItems);
    }

    /**
     * 查询商品评论
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/productComment.do")
    public  ResultBean<List<ProductComment>> getProductComment(Integer prodId,Integer curPageNO) throws Exception{
        List<ProductComment> productComments =  productCommentService.findCommentById(prodId,1);
        return new ResultBean<>(productComments);
    }

}
